import type { Ref, ComponentPublicInstance } from 'vue'
import { __BOTTOM_BAR_HEIGHT__ } from '@/utils/constant'
export default function useInitScrollHeight(
  classNames: string[] | Ref<ComponentPublicInstance | null>[]
): {
  scrollHeight: Ref<number>
} {
  const scrollHeight = ref<number>(0)
  onMounted(calculateHeight)
  if (!window.onresize) {
    window.onresize = calculateHeight
  }
  async function calculateHeight() {
    await nextTick()
    let height = 0
    for (let i = 0; i < classNames.length; i++) {
      const item = classNames[i]
      if (typeof item === 'string') {
        height += document.querySelector(`.${item.split(' ').join(' .')}`)?.clientHeight || 0
      } else {
        height += item.value?.$el?.clientHeight || 0
      }
    }
    scrollHeight.value = window.innerHeight - height - __BOTTOM_BAR_HEIGHT__
  }
  return {
    scrollHeight
  }
}
